<style lang="less" scoped>
.conent{
  padding: 92px 0 10px;
  .row{
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 97px;
    font-size: 26px;
    color: #999;
    border-bottom: 1px solid #333E58;
    padding: 0 35px;
    .send{
      color: #fff
    }
    input{
      background: rgba(0, 0, 0, 0);
      outline: none;
    }
    img{
      width: 33px;
    }
    .hidepwd{
      position: relative;
      top: 2px;
    }
  }
  .actionBtn{
    width: 607px;
    height: 82px;
    background: #FFA22A;
    line-height: 82px;
    text-align: center;
    margin: 73px auto 10px;
    font-size: 30px;
    color: #000
  }
}
</style>

<template>
<div>
  <top-bar/>
  <div class="conent">
    <div class="row">
      <p>{{`${store.state.userMsg.mobile.substring(0, 3)}****${store.state.userMsg.mobile.substring(7)}`}}</p>
      <p></p>
    </div>
    <div class="row">
      <input type="text" placeholder="请输入验证码" v-model="formData.code">
      <p class="send" @click="getCode">{{btnText}}</p>
    </div>
    <div class="row">
      <input :type="showPassword ? 'text' : 'password'" placeholder="新密码" v-model="formData.password">
      <p>
        <img v-show="showPassword" @click="changeShowState(false)" src="../../assets/images/showpsd.png" alt="">
        <img v-show="!showPassword" class="hidepwd" @click="changeShowState(true)" src="../../assets/images/hidepsd.png" alt="">
      </p>
    </div>
    <div class="actionBtn" @click="reset">保存</div>
  </div>
</div>
</template>

<script>
import { useStore } from "vuex"
import { defineComponent, provide, reactive, ref } from "@vue/runtime-core"
import { phoneCode, resetPassword } from '../../api/home'
import { Toast } from "vant"
import { useRouter } from "vue-router"
export default defineComponent({
  setup() {
    const store = useStore()
    provide('title', '修改密码')
    const formData = reactive({
      code: '',
      password: ''
    })
    const router = useRouter()
    let allowGetCode = true
    const btnText = ref('发送')
    const showPassword = ref(false)
    let timer = null
    let time = 60

    const changeShowState = (state) => {
      showPassword.value = state
    }

    const reset = async () => {
      if (!formData.password) {
        Toast.fail('请输入密码')
        return false
      }
      if (!formData.code) {
        Toast.fail('请输入验证码')
        return false
      }
      const loading = Toast.loading('设置中')
      const res = await resetPassword({
        mobile: store.state.userMsg.mobile,
        passWord: formData.password,
        code: formData.code
      })
      loading.clear()
      if (res.code === 200) {
        Toast.success('修改成功')
        try {
          router.go(-1)
        } catch {
          router.push({
            path: '/'
          })
        }
      } else {
        Toast.fail(res.msg)
      }
    }

    const getCode = async () => {
      if (!allowGetCode) {
        return false
      }
      allowGetCode = false
      const res = await phoneCode({ mobile: store.state.userMsg.mobile })
      // if (res.code === 200) {
        time = 60
        timer = setInterval(() => {
          btnText.value = `已发送(${time})`
          if (time > 0) {
            time --
          } else {
            btnText.value = '发送'
            allowGetCode = true
            clearInterval(timer)
          }
        }, 1000)
      // }
    }

    return {
      reset,
      store,
      formData,
      btnText,
      showPassword,
      getCode,
      changeShowState
    }
  }
})
</script>